package com.itheima.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.common.result.Result;
import com.itheima.tlias.mapper.StudentMapper;
import com.itheima.tlias.mapper.ViolationMapper;
import com.itheima.tlias.pojo.dto.SelectStudentDTO;
import com.itheima.tlias.pojo.entity.Student;
import com.itheima.tlias.pojo.entity.Violation;
import com.itheima.tlias.pojo.po.InsertStudentParam;
import com.itheima.tlias.pojo.po.SelectStudentPageParam;
import com.itheima.tlias.pojo.po.UpdateStudentParam;
import com.itheima.tlias.pojo.vo.PageBean;
import com.itheima.tlias.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class SudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ViolationMapper violationMapper;

    @Override
    public Result insertStudent(InsertStudentParam param) {
        param.setUpdateTime(LocalDateTime.now());
        param.setCreateTime(LocalDateTime.now());
        int stuRows = studentMapper.insertStudent(param);
        return stuRows == 0 ? Result.error("新增学员失败") : Result.success();
    }

    @Override
    public Result selectStudentByPage(SelectStudentPageParam param) {
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectStudentDTO> dto = studentMapper.selectStudentByPage(param);
        PageBean<SelectStudentDTO> pageBean = new PageBean<>();
        pageBean.setTotal(page.getTotal());
        pageBean.setRows(dto);
        return Result.success(pageBean);
    }

    @Override
    public Result violation(Integer stuId, Integer score) {
        Violation violation = new Violation();
        violation.setScore(score);
        violation.setStuId(stuId);
        violation.setCreateTime(LocalDateTime.now());
        violation.setUpdateTime(LocalDateTime.now());
        int vioRows = violationMapper.insertViolation(violation);
        return vioRows == 0 ? Result.error("违纪处理失败") : Result.success();
    }

    @Override
    public Result selectStudentById(Integer id) {
        Student student = studentMapper.selectStudentById(id);
        return Result.success(student);

    }

    @Override
    public Result updateStudent(UpdateStudentParam param) {
        SelectStudentDTO dto = null;
        Student student = null;
        dto =new SelectStudentDTO();
        dto.setId(param.getId());
        dto.setNo(param.getNo());
        student = studentMapper.selectStudentUnique(dto);
        if (Objects.nonNull(student)) {
            return Result.error("学号已存在");
        }
        dto =new SelectStudentDTO();
        dto.setId(param.getId());
        dto.setIdCard(param.getIdCard());
        student = studentMapper.selectStudentUnique(dto);
        if (Objects.nonNull(student)) {
            return Result.error("身份证已存在");
        }
        dto =new SelectStudentDTO();
        dto.setId(param.getId());
        dto.setPhone(param.getPhone());
        student = studentMapper.selectStudentUnique(dto);
        if (Objects.nonNull(student)) {
            return Result.error("手机号已存在");
        }
        param.setUpdateTime(LocalDateTime.now());
        int stuRows = studentMapper.updateStudent(param);
        return stuRows == 0 ? Result.error("更新学员失败") : Result.success();
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result batchDeleteStudentByIds(List<Integer> ids) {
        int stuRows = studentMapper.batchDeleteStudentByIds(ids);
        int vioRows =violationMapper.delectViolationByStuIds(ids);
        return stuRows ==0 ? Result.error("删除学员失败") : Result.success();
    }
}
